<template>
  <div
    title="全屏"
    class="icon-warp"
    @click="handleClick"
  >
    <i
      class="iconfont  font-style"
      :class="{'icon-quanping':!isFullscreen , 'icon-quxiaoquanping':isFullscreen}"
    />
  </div>
</template>

<script lang="ts">
import screenfull from 'screenfull';
import {
  defineComponent,
  reactive,
  toRefs,
  onBeforeUnmount,
  onMounted,
} from 'vue';

export default defineComponent({
  name: 'Screenfull',
  setup() {
    const data = reactive({
      isFullscreen: false,
    });

    function handleClick(): boolean {
      if (!screenfull.isEnabled) {
        this.$message({
          message: 'Your browser does not support it',
          type: 'warning',
        });
        return false;
      }
      screenfull.toggle();
    }

    function change() {
      data.isFullscreen = screenfull.isFullscreen;
    }

    function init() {
      if (screenfull.isEnabled) {
        screenfull.on('change', change);
      }
    }

    //组件写在生命周期
    onBeforeUnmount(() => {
      if (screenfull.isEnabled) {
        screenfull.on('change', change);
      }
    });
    //加载完成生命周期
    onMounted(() => {
      init();
    });
    return {
      ...toRefs(data),
      handleClick,
    };
  },
});
</script>

<style lang="scss" scoped>
.icon-warp {
  width: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  &:hover .font-style {
    font-size: 26px;
  }
}

.font-style {
  color: var(--laout-header-color);
  font-size: 20px;
  cursor: pointer;
  transition: 0.2s;
}
</style>